package com.example

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.livedata.EventConfig
import com.example.network.BaseOkhttpClient
import kotlinx.coroutines.launch

/**
 * author：jiazhijie
 * date：2025/10/16
 * desc：
 * modify record：
 */
class LoginVm(
    application: Application
) : AndroidViewModel(application) {

    val liveData = MutableLiveData<Int>()

    fun sendRequestWithOkHttp(
        email: String,
        password: String,
        block: ((Boolean) -> Unit)? = null
    ) {
        viewModelScope.launch {
            val bodyMap = mapOf(
                "username" to email,
                "password" to password
            )

            val responseData = BaseOkhttpClient.postRequest(
                "/api/jack/login",
                bodyMap
            ).toString()

            Log.i("responseData is", responseData)
            if (responseData.contains("true")) {
                liveData.value = EventConfig.EVENT_LOGIN_SUCCESS
                block?.invoke(true)
            } else {
                block?.invoke(false)
                liveData.value = EventConfig.EVENT_LOGIN_FAIL

            }
        }
    }
}
